
var tags = ["","","",""];
var teachers = ["","","",""];
var siglas = ["","","",""];
var courseNames = ["","","",""];
var semesters = ["","","",""];
var types = ["","","",""];
var descriptions = ["","","",""];
function openMenu(element1, name)
{
	var element = document.getElementById(name);
	var areAllValidated = true;
	for(i=1; i<5; i++)
	{
		var iterator = i.toString();
		var ulcontent = "uploadcontent"+iterator;
		var filecontent = "file"+iterator
		document.getElementById(ulcontent).style.display = "none";
		var singleValidated = validate(iterator);
		areAllValidated = areAllValidated && singleValidated;
		if(singleValidated)
		{
			document.getElementById(filecontent).style.backgroundColor = "LightGreen";
		}
		else
		{
			document.getElementById(filecontent).style.backgroundColor = "FF3333";
		}
	}
	uploadButton.disabled = !areAllValidated;
	if(element.style.display == "")
	{
		element.style.display = "none";
	}
	else
	{
		element1.style.backgroundColor="LightYellow";
		element.style.display="";
	}
}
function eraseText(element)
{
	if(element.value=="Escriba una descripcion corta aqui...")
	{
		element.value="";
	}
}

function addTeacher(element, position)
{
	var text = document.getElementById(element).value;
	addTeachers(position,text);
	return false;
}

function addTeachers(position,text)
{
	if(text != "")
	{
		var teach = teachers[position];
		if(teach == "")
		{
			teach = "Profesores: "+text;
		}
		else
		{
			teach = teach + ", " + text;
		}
		teachers[position] = teach;
		document.getElementById("teachersLabel"+(position+1).toString()).innerHTML=teach;
	}
}

function addTags(element, position)
{
	var text = document.getElementById(element).value;
	addTag(position, text);
	return false;
}

function addTag(position, text)
{
	if(text != "")
	{
		var tag = tags[position];
		if(tag == "")
		{
			tag = "Tags: "+text;
		}
		else
		{
			tag = tag + ", " + text;
		}
		tags[position] = tag;
		document.getElementById("tagsLabel"+(position+1).toString()).innerHTML=tag;
	}
}

function showFiles()
{
	filelist.style.display="";
}

function validate(iterator)
{
	return (document.getElementById("sigla"+iterator).value != "" && document.getElementById("courseName"+iterator).value != "" && document.getElementById("type"+iterator).value != "" && document.getElementById("semester"+iterator).value != "");

}

function upload()
{
	return false;
}

var isCtrl = false;
var isAlt = false;

function keyUp(e) 
{
	if(e.which == 17) isCtrl=false;
	else if(e.which == 18) isAlt = false;
}

function autoFillSigla(e, element)
{
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 71 && isCtrl && isAlt) { //CTRL + ALT + G
		for(i=0; i<4; i++)
		{
			siglas[i]=element.value
		}
		saveValues("sigla");
		updateForms();
		return false;
	}
}

function autoFillCourseName(e, element)
{
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 71 && isCtrl && isAlt) { //CTRL + ALT + G
		for(i=0; i<4; i++)
		{
			courseNames[i]=element.value
		}
		saveValues("courseName");
		updateForms();
		return false;
	}
}

function autoFillSemester(e, element)
{
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 71 && isCtrl && isAlt) { //CTRL + ALT + G
		for(i=0; i<4; i++)
		{
			semesters[i]=element.value
		}
		saveValues("semester");
		updateForms();
		return false;
	}
}

function autoFillType(e, element)
{
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 71 && isCtrl && isAlt) { //CTRL + ALT + G
		for(i=0; i<4; i++)
		{
			types[i]=element.value
		}
		saveValues("type");
		updateForms();
		return false;
	}
}

function autoFillTeacher(e, element)
{
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 71 && isCtrl && isAlt) { //CTRL + ALT + G
		for(i=0; i<4; i++)
		{
			addTeachers(i, element.value);
		}
		saveValues("");
		updateForms();
		return false;
	}
}

function autoFillTag(e, element)
{
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 71 && isCtrl && isAlt) { //CTRL + ALT + G
		for(i=0; i<4; i++)
		{
			addTag(i, element.value);
		}
		saveValues("");
		updateForms();
		return false;
	}
}

function updateForms()
{
	for(i=0; i<4; i++)
	{
		document.getElementById("sigla"+(i+1).toString()).value=siglas[i];
		document.getElementById("courseName"+(i+1).toString()).value=courseNames[i];
		document.getElementById("semester"+(i+1).toString()).value=semesters[i];
		document.getElementById("type"+(i+1).toString()).value=types[i];
		document.getElementById("teachersLabel"+(i+1).toString()).innerHTML=teachers[i];
		document.getElementById("tagsLabel"+(i+1).toString()).innerHTML=tags[i];
	}
}

function saveValues(doNotSave)
{
	for(i=0; i<4; i++)
	{
		if(doNotSave!="sigla")
		{
			siglas[i]=document.getElementById("sigla"+(i+1).toString()).value;
		}
		if(doNotSave!="courseName")
		{
			courseNames[i]=document.getElementById("courseName"+(i+1).toString()).value;
		}
		if(doNotSave!="semester")
		{
			semesters[i]=document.getElementById("semester"+(i+1).toString()).value;
		}
		if(doNotSave!="semester")
		{
			types[i]=document.getElementById("type"+(i+1).toString()).value;
		}
	}
}

$(function(){
	// Tabs
	$('#tabs').tabs();

});

